

/**
 * entity对象基类
 */
import { v4 as uuid } from 'uuid'
import OverlayEvent from '../event/types/OverlayEvent'

export default class Entity {
  constructor() {
    this._id = uuid()
    this._attr = {}
    this.delegate = undefined
    this._style = undefined
    this._show = true
    this._type = undefined
    this._layer = undefined
    this._rotateAmount = 0
    // 模型旋转相关
    this._heading = 0
    this._pitch = 0
    this._roll = 0

    this._overlayEvent = new OverlayEvent()
  }

  get overlayEvent() {
    return this._overlayEvent
  }

  get layer() {
    return this._layer
  }

  get heading() {
    return this._heading
  }

  set heading(val) {
    this._heading = val
    this.setRotate()
  }

  get pitch() {
    return this._pitch
  }

  set pitch(val) {
    this._pitch = val
    this.setRotate()
  }

  get roll() {
    return this._roll
  }

  set roll(val) {
    this._roll = val
    this.setRotate()
  }

  get rotateAmount() {
    return this._rotateAmount
  }

  set rotateAmount(val) {
    this._rotateAmount = val
  }

  get id() {
    return this._id
  }

  get type() {
    return this._type
  }

  get show() {
    return this._show
  }

  set show(val) {
    this._show = val
    this.delegate.show = this._show
  }
  /**
  * 
  * @param {Number} angle 设置旋转角度 
  */
  startRotate(angle = 1) {
    this.rotateAmount = angle
    this.delegate.orientation = new Cesium.CallbackProperty((time, result) => {
      this._heading += this.rotateAmount

      if (this._heading >= 360 || this._heading <= -360) {
        this._heading = 0
      }
      return Cesium.Transforms.headingPitchRollQuaternion(
        this._positions,
        new Cesium.HeadingPitchRoll(
          Cesium.Math.toRadians(this._heading),
          Cesium.Math.toRadians(this._pitch),
          Cesium.Math.toRadians(this._roll)
        )
      )
    }, false)
  }


  /**
  * 停止旋转
  */
  stopRotate() {
    this.delegate.orientation = new Cesium.CallbackProperty((time, result) => {
      return Cesium.Transforms.headingPitchRollQuaternion(
        this._position,
        new Cesium.HeadingPitchRoll(
          Cesium.Math.toRadians(this.heading),
          Cesium.Math.toRadians(this.pitch),
          Cesium.Math.toRadians(this.roll)
        )
      )
    })
  }


  /**
   * 模型角度更改 自动设置朝向
   */
  setRotate() {
    this.delegate.orientation = Cesium.Transforms.headingPitchRollQuaternion(
      this._positions,
      new Cesium.HeadingPitchRoll(
        Cesium.Math.toRadians(this.heading),
        Cesium.Math.toRadians(this.pitch),
        Cesium.Math.toRadians(this.roll)
      )
    )
  }

  /**
   * 设置位置
   */
  setPosition() {
    this.delegate.orientation = Cesium.Transforms.headingPitchRollQuaternion(
      this._positions,
      new Cesium.HeadingPitchRoll(
        Cesium.Math.toRadians(this.heading),
        Cesium.Math.toRadians(this.pitch),
        Cesium.Math.toRadians(this.roll)
      )
    )
  }

  /**
   * 
   * @param {*} text 文字 
   * @param {*} textStyle 文字样式 
   */
  setLabel(text, textStyle = {}) {
    if (!text) return
    this.delegate.label = {
      text,
      eyeOffset: new Cesium.Cartesian3(0, 100, 0),
      fillColor: Cesium.Color.DEEPSKYBLUE,
      font: 10,
      ...textStyle
    }
  }

  /**
   * 转换为json文件供加载使用
   */
  toGeoJSON() { }

  /**
   * 绑定图元事件
   */
  on(type, fn) {
    this._overlayEvent.on(type, fn)
  }

  /**
   * 删除事件
   */
  off(type, fn) {
    this._overlayEvent.off(type, fn)
  }

  /**
   * 当entity被添加到layer图层时 为其增加所有的事件
   */

  emit() {
    this._overlayEvent._setInputaction(this)
  }
}
